# -*- coding: utf-8 -*-
import time
import numpy as np
import pandas as pd
from multiprocessing import Process
import threading
from .orderbook import OrderBook
from contract_market.base.engine import EventEngineMeta


class ImageMarketEngine(EventEngineMeta):

    def __init__(self, event_center, event_engine, *, configure, back) -> None:
        """
        对于多个市场同时启动或停止的管理器
        ::param depth_configure
        ::param event_center
        ::param event_engine
        """
        super().__init__(event_center, event_engine)
        self.event_center, self.event_engine = event_center, event_engine
        self.back = back
        #
        self.orderbook = OrderBook(configure, self.event_center, self.event_engine, back)
        # self.event_engine.callback.event.set()

    def stop(self):
        """ """

    def start(self, method='thread'):
        """ """
        print("[ImageMarketEngine] - start")

        if method == 'process':
            funcs = [
                self.orderbook.start_price_sync, 
                self.orderbook.start_quantity_sync, 
                self.orderbook.start_trades,
                self.orderbook.start_sub_density
                ]
            procs = [Process(target=func) for func in funcs]
            for p in procs:
                p.start()
            for p in procs:
                p.join()
        elif method == 'thread':
            jobs = [
                # sync
                self.orderbook.start_user_long_price_sync,
                self.orderbook.start_mt_long_price_sync,
                self.orderbook.start_user_short_price_sync,
                self.orderbook.start_mt_short_price_sync,
                # density
                self.orderbook.start_user_sub_density,
                self.orderbook.start_mt_sub_density,
                # overflow
                self.orderbook.start_long_overflow,
                self.orderbook.start_short_overflow,
                # trade
                self.orderbook.start_trades,
                # aux
                self.orderbook.start_aux_orderbook,
                self.orderbook.start_activate_orderbook,
                self.orderbook.start_quantity_sync,
                # optimal
                self.orderbook.start_risk_metrics,
                self.orderbook.start_optimize,
                self.orderbook.start_timer,
                self.orderbook.start_controller,

                # subproc
                # self.orderbook.start_user_sub_density,
                # self.orderbook.start_aux_orderbook,
                # self.orderbook.start_long_intersection,
                # self.orderbook.start_short_intersection,
                # self.orderbook.start_price_overflow,
            ]
            ths = [threading.Thread(target=job, daemon=True) for job in jobs]
            for t in ths:
                t.start()
            for t in ths:
                t.join()
        else:
            self.orderbook.start_price_sync()
            self.orderbook.start_sub_density()
            self.orderbook.start_trades()

